package wtx.geek;

import java.util.*;
import java.util.Random;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.Future;

import com.google.common.base.Joiner;
import javafx.util.Pair;
import sun.plugin.dom.exception.InvalidStateException;

/**
 * input: [-1, 1, 3, -2, 2]
 * output: [-1, -2, 1, 3, 2]
 * 负数在前，正数在后，都按照各自原来的顺序排列
 * https://careercup.com/question?id=5201559730257920
 **/

public class Solution {
  public static void resortArray(int[] array) {
    final int len = array.length;
    int p0 = 0, p1 = 1;
    while(p0 < len && p1 < len) {
      if (array[p0] > 0) {
        while (p1 < len && array[p1] > 0) {
          ++p1;
        }
        if (p1 >= len) break;
        for (int i = p1; i > p0; --i) {
          int tmp = array[i];
          array[i] = array[i-1];
          array[i-1] = tmp;
        }
      }
      ++p0; ++p1;
    }
  }

  public static void main(String[] args) {

  }
}
